package com.aduerr.v1shouyin.mvp.adpter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.UseKouBeiCardListEntity;
import com.aduerr.v1shouyin.util.LogUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fyf on 2018/6/6 0006.
 */

public class KouBeihexiaoStickrecycleadpter extends RecyclerView.Adapter implements StickyRecyclerHeadersAdapter {
    private Context context;
    private List<UseKouBeiCardListEntity.DataBean> outdataBeen = new ArrayList<>();
    private List<UseKouBeiCardListEntity.DataBean.ListBean> inlist = new ArrayList<>();
    private ViewHolder viewHolder;
    private HeaadViewHolder heaadViewHolder;

    public KouBeihexiaoStickrecycleadpter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView =
                LayoutInflater.from(context).inflate(R.layout.item_testcoupons_list_recycler, parent, false);
        viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(inlist.size() > 0) {
            viewHolder = (ViewHolder) holder;
            viewHolder.tvPayJiner.setText(inlist.get(position).getName());
            viewHolder.tvPayNicheng.setText(inlist.get(position).getSiteusername());
            viewHolder.tvPayState.setText(inlist.get(position).getIsuse() == 1 ?
                    "核销成功" : "未核销");
            viewHolder.tvPayTime.setText(inlist.get(position).getUsetime().substring(10
                    , 19));
            String paytype = inlist.get(position).getType();
            if(TextUtils.equals(paytype, "0")) {//优惠
                viewHolder.ivKaquanIconDetail.setImageResource(R.drawable.img_card_youhui);
            } else {
                if(TextUtils.equals(paytype, "1")) {
                    viewHolder.ivKaquanIconDetail.setImageResource(R.drawable.img_card_diyong);
                } else if(TextUtils.equals(paytype, "2")) {
                    viewHolder.ivKaquanIconDetail.setImageResource(R.drawable.img_card_zhekou);
                } else if(TextUtils.equals(paytype, "3")) {
                    viewHolder.ivKaquanIconDetail.setImageResource(R.drawable.img_card_daijin);
                } else if(TextUtils.equals(paytype, "4")) {
                    viewHolder.ivKaquanIconDetail.setImageResource(R.drawable.img_card_lipin);
                }
            }
        }
    }

    @Override
    public long getHeaderId(int position) {
        if(inlist.size() > 0) {

            return Math.abs(inlist.get(position).getDate().hashCode());
        } else {
            return -1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_pmr_header,
                parent, false);
        heaadViewHolder = new HeaadViewHolder(view);
        return heaadViewHolder;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(inlist.size() > 0) {
            heaadViewHolder = (HeaadViewHolder) holder;
            heaadViewHolder.tvTime.setText(inlist.get(position).getDate());
        }
    }

    @Override
    public int getItemCount() {
        return inlist.size();
    }


    public void refresh(List<UseKouBeiCardListEntity.DataBean> dataBeen) {
        outdataBeen.clear();
        outdataBeen.addAll(dataBeen);
        if(outdataBeen.size() == 0) {
            ToastUtils.showToast(context, "没有数据");
        } else {
            inlist.clear();
            for(int i = 0; i < outdataBeen.size(); i++) {
                UseKouBeiCardListEntity.DataBean dataBean = outdataBeen.get(i);
                List<UseKouBeiCardListEntity.DataBean.ListBean> list = dataBean.getList();
                if(list != null) {//内部的数据
                    for(int j = 0; j < list.size(); j++) {
                        inlist.add(list.get(j));
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    //加载更多
    public void loadmore(List<UseKouBeiCardListEntity.DataBean> dataBeen) {
        LogUtils.e("outdataBeen.size",
                "================================" + outdataBeen.size());
        outdataBeen.addAll(dataBeen);
        LogUtils.e("outdataBeen.size",
                "================================" + outdataBeen.size());
        if(outdataBeen.size() == 0) {
            ToastUtils.showToast(context, "没有数据");
        } else {
            for(int i = 0; i < outdataBeen.size(); i++) {
                UseKouBeiCardListEntity.DataBean dataBean = outdataBeen.get(i);
                List<UseKouBeiCardListEntity.DataBean.ListBean> list = dataBean.getList();
                if(list != null) {//内部的数据
                    for(int j = 0; j < list.size(); j++) {
                        inlist.add(list.get(j));
                    }
                }
            }
        }
        notifyDataSetChanged();
    }


    //普通的条目
    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_kaquan_icon_detail)
        ImageView ivKaquanIconDetail;
        @BindView(R.id.tv_pay_jiner)
        TextView tvPayJiner;
        @BindView(R.id.tv_pay_nicheng)
        TextView tvPayNicheng;
        @BindView(R.id.tv_pay_time)
        TextView tvPayTime;
        @BindView(R.id.tv_pay_state)
        TextView tvPayState;
        @BindView(R.id.ll_containt_detail)
        RelativeLayout llContaintDetail;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    //头部
    static class HeaadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.iv_togo_item_pmr_header)
        ImageView ivTogoItemPmrHeader;
        @BindView(R.id.top_rl)
        RelativeLayout topRl;

        HeaadViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }
}

